<?php

class TagController extends FrontendController
{
    /**
     * @throws CHttpException
     */
	public function actionIndex()
	{
        $model=ArticleMetaTag::model()->findAll();
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
		$this->render('index',array(
			'model'=>$model,
		));
	}

    /**
     * @param $name
     * @throws CHttpException
     */
    public function actionView($name)
    {
        if(!$name)
            throw new CHttpException(404,'The requested page does not exist.');

        //$model = Article::model()->findAll(array('condition'=>'articleTags.value=:tag','with'=>'articleTags', 'params'=>array(':tag'=>urldecode($name))));
        $criteria = new CDbCriteria();
        $criteria->order = 't.sort ASC, t.id DESC';
        $criteria->condition = 'articleTags.`value` = \''.urldecode($name).'\'';
        $criteria->join = 'LEFT JOIN fcms_article_meta AS articleTags ON t.id = articleTags.aid';
        $criteria->with = 'articleTags';
        //$criteria->params = array(':tag'=>urldecode($name));
        $pageCount = Article::model()->count(array('condition'=>'articleTags.value=:tag','with'=>'articleTags', 'params'=>array(':tag'=>urldecode($name))));
        $pages = new CPagination($pageCount);
        $pages->pageSize = 8;
        $pages->applyLimit($criteria);
        $model = Article::model()->findAll($criteria);
        //var_dump($model);//exit;

        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        $this->render('view',array(
            'model'=>$model,
            'pager'=>$pages,
            'name'=>urldecode($name),
        ));
    }


}
